package com.atjava.webspider.gui.page;

import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.atjava.webspider.gui.site.NewSiteGui;
import com.atjava.webspider.logic.CommonLogic;
import com.atjava.webspider.model.Page;
import com.atjava.webspider.model.Site;
import com.atjava.webspider.services.TableService;
import com.atjava.webspider.util.DialogUtil;

public class NewPageGui extends JDialog {

	private static final long serialVersionUID = 1L;
	
	private JPanel jContentPane = null;
	private JLabel jLabelName = null;
	private JLabel jLabelLevel = null;
	private JTextField jTextFieldName = null;
	private JTextField jTextFieldLevel = null;
	private JButton jButtonSave = null;
	private JButton jButtonReset = null;
	private Page page;
	/**
	 * @param owner
	 */
	public NewPageGui(JDialog owner,boolean modal,Page page) {
		super(owner,modal);
		this.page = page;
		initialize();
		setTextValue();
	}
	
	public NewPageGui(JDialog owner,boolean modal) {
		this(owner,modal,new Page());
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setBounds(new Rectangle(0, 0, 360, 113));
		this.setResizable(false);
		this.setTitle("新建页面");
		this.setLocationRelativeTo(getOwner());
		this.setContentPane(getJContentPane());
	}
	
	private void setTextValue(){
		if(page == null)
			return ;
		try {
			if(StringUtils.isNotBlank(page.getName())){
				jTextFieldName.setText(page.getName());
				jTextFieldName.setEnabled(false);
			}
			
			if(page.getLevel() != 0)
				jTextFieldLevel.setText(String.valueOf(page.getLevel()));
			
		} catch (Exception e) {
			e.printStackTrace();
			DialogUtil.showError(this, "加载页面失败！");
		}
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabelLevel = new JLabel();
			jLabelLevel.setBounds(new Rectangle(235, 10, 65, 25));
			jLabelLevel.setText("页面级别：");
			jLabelName = new JLabel();
			jLabelName.setBounds(new Rectangle(10, 10, 65, 25));
			jLabelName.setText("页面名称：");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabelName, null);
			jContentPane.add(jLabelLevel, null);
			jContentPane.add(getJTextFieldName(), null);
			jContentPane.add(getJTextFieldLevel(), null);
			jContentPane.add(getJButtonSave(), null);
			jContentPane.add(getJButtonReset(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextFieldName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldName() {
		if (jTextFieldName == null) {
			jTextFieldName = new JTextField();
			jTextFieldName.setBounds(new Rectangle(75, 10, 150, 25));
		}
		return jTextFieldName;
	}

	/**
	 * This method initializes jTextFieldPath	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldLevel() {
		if (jTextFieldLevel == null) {
			jTextFieldLevel = new JTextField();
			jTextFieldLevel.setBounds(new Rectangle(300, 10, 45, 25));
		}
		return jTextFieldLevel;
	}

	/**
	 * This method initializes jButtonSave	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSave() {
		if (jButtonSave == null) {
			jButtonSave = new JButton();
			jButtonSave.setBounds(new Rectangle(195, 47, 70, 25));
			jButtonSave.setText("保存");
			jButtonSave.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					page.setName(NewPageGui.this.jTextFieldName.getText());
					String lev = NewPageGui.this.jTextFieldLevel.getText();
					page.setLevel(Byte.valueOf(lev));
					//add into bean
					NewSiteGui newTaskGui = (NewSiteGui)NewPageGui.this.getOwner();
					//add into table
					try {
						Site st = newTaskGui.getSite();
						if(st.getPages() == null)
							st.setPages(new ArrayList<Page>());
						int idx = CommonLogic.getIndexByName(st.getPages(),page.getName());
						TableService tabService = TableService.getInstance();
						//更新
						if(idx >= 0){
							st.getPages().set(idx, page);
							newTaskGui.getOutputModel().updateData(idx, tabService.getPageRow(page));
						//增加
						}else{
							st.getPages().add(page);
							newTaskGui.getOutputModel().addData(tabService.getPageRow(page));
						}
					} catch (Exception exc) {
						exc.printStackTrace();
					}
					NewPageGui.this.setVisible(false);
				}
			});
		}
		return jButtonSave;
	}
	
	/**
	 * This method initializes jButtonReset	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReset() {
		if (jButtonReset == null) {
			jButtonReset = new JButton();
			jButtonReset.setBounds(new Rectangle(276, 47, 70, 25));
			jButtonReset.setText("取消");
			jButtonReset.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					NewPageGui.this.setVisible(false);
				}
			});
		}
		return jButtonReset;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
